<template>
  <div style="width:100%;display: flex;background-color: #fff;">
    <div style="width:100%;padding: 30px;">
      <a-form class="smart-query-form" v-privilege="'oa:enterprise:query'">
        <a-row class="smart-query-form-row">
          <a-form-item label="设备名称" name="deviceName">
            <a-input class="form-item" v-model:value.trim="queryForm.deviceName" placeholder="请输入设备名称" />
          </a-form-item>
          <a-form-item label="设备编号" name="serialNumber">
            <a-input class="form-item" v-model:value.trim="queryForm.serialNumber" placeholder="请输入设备编号" />
          </a-form-item>
          <a-form-item label="设备状态" name="deviceStatus">
            <a-select style="width: 160px" v-model:value="queryForm.status" placeholder="请选择"
              :options="[{ value: '1', label: '未激活' }, { value: '2', label: '禁用' }, { value: '3', label: '在线' }, { value: '4', label: '离线' }]">
            </a-select>
            <a-checkbox v-model:checked="queryForm.showChild" @click="checkTop">显示上级机构数据</a-checkbox>
            <a-tooltip placement="topLeft">
              <template #title>
                <span>选中后，本级可以看下级的数据</span>
              </template>
              <SlackSquareOutlined />
            </a-tooltip>
          </a-form-item>
          <a-form-item class="smart-query-form-item smart-margin-left10">
            <a-button-group>
              <a-button type="primary" @click="onSearch">
                <template #icon>
                  <SearchOutlined />
                </template>
                查询
              </a-button>
              <a-button @click="resetQuery">
                <template #icon>
                  <ReloadOutlined />
                </template>
                重置
              </a-button>
            </a-button-group>
          </a-form-item>
        </a-row>
      </a-form>
      <a-row :gutter="3">
        <a-col :span="8" v-for="(item, index) in arr" style="padding: 10px;">
          <a-card hoverable class="content">
            <div class="card-title">
              <span class="deviceName">{{ item.deviceName }}</span>
              <a-button size="small" class="statusBtn">
                <span>{{ ['未激活', '禁用', '在线', '离线'][item.status - 1] }}</span>
              </a-button>
              <div class="qRCode" @click="clickCode(item)">
                <QrcodeOutlined class="icons" />
              </div>
              <div class="wifiCode">
                <img src="/@/assets/images/device/wifi.svg">
                <!-- <WifiOutlined class="icons"/> -->
              </div>
            </div>
            <div style="width: 100%">
              <a-row :gutter="3">
                <a-col :span="14">
                  <a-tag class="tagBtn" color="blue"><span>{{ item.protocolCode }}</span></a-tag>
                  <a-tag class="tagBtn" color="blue"><span>{{ item.transport }}</span></a-tag>
                  <div class="card-content">
                    <a-descriptions-item>
                      <span>编号:</span>
                      <span style="color: #0147EB;"> {{ item.serialNumber }}</span>
                      <br />
                      <span>产品: {{ item.productName }}</span>
                      <br />
                      <span>激活时间: {{ item.activeTime }}</span>
                      <br />
                    </a-descriptions-item>
                  </div>
                </a-col>
                <a-col :span="8">
                  <img src="/@/assets/images/device/video.svg" v-if="item.deviceType === 1">
                  <img src="/@/assets/images/device/gateway.svg" v-if="item.deviceType === 2">
                  <img src="/@/assets/images/device/video.svg" v-if="item.deviceType === 3">
                  <img src="/@/assets/images/device/product.svg" v-if="item.deviceType === 4">
                </a-col>
              </a-row>
            </div>

            <div style="padding-top: 16px;">
              <a-button class="btn" @click="clickbtnStatus(item)">运行状态</a-button>
            </div>
          </a-card>
        </a-col>
      </a-row>
      <div style="bottom: 0px;width: 100%;text-align: right">
        <a-pagination show-size-changer v-model:current="queryForm.pageNum" v-model:pageSize="queryForm.pageSize"
          :page-size-options="pageSizeOptions" :total="total" @change="queryData" @showSizeChange="onShowSizeChange" />
        <br />
      </div>
    </div>
    <a-modal v-model:visible="qrCodevisible" style="width: 400px;" :footer="null">
      <div style="text-align: center">
        <vue-qr id="payQR" :text="codeText" :size="300">
        </vue-qr>
      </div>
    </a-modal>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import { smartSentry } from "/@/lib/smart-sentry.js";
import { deviceApi } from "/src/api/device/device.js";
import { useRouter } from 'vue-router';
import vueQr from 'vue-qr/src/packages/vue-qr.vue';
const router = useRouter();
const formDefault = {
  serialNumber: '',
  deviceName: '',
  status: '',
  showChild: true,
  pageNum: 1,
  pageSize: 6,
};
let queryForm = reactive({ ...formDefault });
const arr = ref([{}])
const total = ref()
const pageNum = ref(1);
const pageSize = ref(6);
const pageSizeOptions = ref(['12', '18', '24', '30', '36']);
const qrCodevisible = ref(false);
const codeText = ref("")
function clickCode(val) {

  let obj = {
    deviceId: val.deviceId,
    productName: val.productName,
    serialNumber: val.serialNumber,
    createTime: val.createTime
  }
  codeText.value = JSON.stringify(obj)
  qrCodevisible.value = true
}
//分页查询页面
async function queryData() {
  try {
    let queryResult = await deviceApi.shortList(queryForm);

    arr.value = queryResult.data.rows;
    total.value = queryResult.data.total;
  } catch (e) {
    smartSentry.captureError();
  } finally {
    console.log()
  }
}
onMounted(queryData);

const clickbtnStatus = (val) => {
  router.push({
    path: '/device/status',
    query: {
      deviceId: val.deviceId,
      serialNumber: val.serialNumber
    }
  });
}
// 查询
function onSearch() {
  queryForm.pageNum = 1;
  queryData();
}
// 重置查询
function resetQuery() {
  // searchDate.value = [];
  Object.assign(queryForm, formDefault);
  queryData();
}
//checke复选框
function checkTop() {
  queryForm.pageNum = 1;
  if (queryForm.showChild) {
    queryForm.showChild = false;
  } else {
    queryForm.showChild = true;
  }
  queryData();
}
function onShowSizeChange() {
  console.log(pageNum.value, pageSize.value);
  queryData();
};


/*const arr = ref([{
  name: '1',
  type: 1
}, {
  name: '1',
  type: 1
}, {
  name: '1',
  type: 3
}, {
  name: '1',
  type: 4
}, {
  name: '1',
  type: 5
}])*/

</script>

<style scoped>
.content {
  background-image: linear-gradient(#e9f1fc, #fefefe);
}

/*img{
  width: 120px;
  height: 120px;
}*/

.qRCode {
  margin-left: 26px;
}

.wifiCode {
  margin-left: 8px;
}

.icons {
  font-size: 20px;
  color: #C0C4CC;
  margin-left: 15px;
}

.card-content {
  margin: 6px 10px 0px 0px;
}

span {
  font-size: 12px;
  line-height: 1.5;
  color: #606266;
  letter-spacing: 1px;
  padding: 1px 1px 8px;

}

.card-title {
  display: flex;
  flex-direction: row;
  align-items: center
}

.deviceName {
  font-size: 16px;
  letter-spacing: 1px;
  line-height: 32px;
  margin-right: 0.5em;
  cursor: pointer;
  font-weight: bold;
  color: #000;
  font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
}

.card-title {
  padding-bottom: 20px;
}

/*MQTT JSON等状态按钮*/
.tagBtn {
  padding: 2px;
}

/*状态按钮（1-未激活 2-禁用 3-在线 4-离线）*/
.statusBtn {
  background-color: #f0efe84f;
}

/*运行状态按钮*/
.btn {
  width: 80px;
  height: 30px;
  font-size: 12px;
  float: right;
}

/*搜索框与框之间的距离*/
:where(.css-dev-only-do-not-override-1r8ax8d).ant-form-horizontal.ant-form-item-label {
  padding-left: 6px;
}

:where(.css-dev-only-do-not-override-1r8ax8d).ant-btn-primary.ant-btn-background-ghost {
  width: 50px;
  height: 30px;
  font-size: 12px;
  text-align: center;
}

.smart-query-form-row .smart-query-form-item {
  margin-top: 0px;
}
</style>
